package com.seocoo.worker.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seocoo.worker.R;

/**
 * author : Ssp
 * e-mail : 981297079@qq.com
 * date   : 2020/2/2422:59
 * desc   :
 * version: 1.0
 */
public class AppToolBer extends LinearLayout implements View.OnClickListener {

    private TextView tvTitle;
    private TextView tvRight;
    private TextView tvLeft;

    public AppToolBer(@NonNull Context context) {
        this(context, null);
    }

    public AppToolBer(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppToolBer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(@NonNull AttributeSet attrs) {

        this.setGravity(Gravity.CENTER);
        this.setOrientation(HORIZONTAL);

        this.tvTitle = new TextView(getContext());
        this.tvRight = new TextView(getContext());
        this.tvLeft = new TextView(getContext());


        LayoutParams left = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        left.gravity = Gravity.CENTER | Gravity.LEFT;
        this.tvLeft.setGravity(Gravity.CENTER);
        this.tvLeft.setPadding(40, 0, 15, 0);
        this.tvLeft.setCompoundDrawablePadding(10);
        this.tvLeft.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        this.tvLeft.setOnClickListener(this);
        this.addView(tvLeft, left);

        LayoutParams title = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        title.gravity = Gravity.CENTER;
        title.weight = 1;
        this.tvTitle.setGravity(Gravity.CENTER);
        this.tvTitle.setPadding(10, 0, 10, 0);
        this.tvTitle.setCompoundDrawablePadding(10);
        this.tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        this.addView(tvTitle, title);

        LayoutParams right = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        right.gravity = Gravity.CENTER | Gravity.RIGHT;
        this.tvRight.setGravity(Gravity.CENTER);
        this.tvRight.setPadding(15, 0, 40, 0);
        this.tvRight.setCompoundDrawablePadding(10);
        this.tvRight.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        this.addView(tvRight, right);

        this.initStyle(attrs);
    }

    private void initStyle(AttributeSet attrs) {
        TypedArray attributes = this.getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.baseAppToolBerView, 0, 0);
        if (attributes == null) {
            return;
        }

        String leftTxt = attributes.getString(R.styleable.baseAppToolBerView_leftTxt);
        float leftSize = attributes.getDimension(R.styleable.baseAppToolBerView_leftSize, 30);
        int leftColor = attributes.getColor(R.styleable.baseAppToolBerView_leftColor, Color.BLACK);
        Drawable leftIcon = attributes.getDrawable(R.styleable.baseAppToolBerView_leftIcon);
        this.tvLeft.setText(leftTxt);
        this.tvLeft.setTextColor(leftColor);
        this.tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftSize);
        this.tvLeft.setCompoundDrawablesWithIntrinsicBounds(leftIcon, null, null, null);

        String rightTxt = attributes.getString(R.styleable.baseAppToolBerView_rightTxt);
        float rightSize = attributes.getDimension(R.styleable.baseAppToolBerView_rightSize, 30);
        int rightColor = attributes.getColor(R.styleable.baseAppToolBerView_rightColor, Color.BLACK);
        Drawable rightIcon = attributes.getDrawable(R.styleable.baseAppToolBerView_rightIcon);
        this.tvRight.setText(rightTxt);
        this.tvRight.setTextColor(rightColor);
        this.tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightSize);
        this.tvRight.setCompoundDrawablesWithIntrinsicBounds(rightIcon, null, null, null);

        String titleTxt = attributes.getString(R.styleable.baseAppToolBerView_titleTxt);
        float titleSize = attributes.getDimension(R.styleable.baseAppToolBerView_titleSize, 30);
        int titleColor = attributes.getColor(R.styleable.baseAppToolBerView_titleColor, Color.BLACK);
        this.tvTitle.setText(titleTxt);
        this.tvTitle.setTextColor(titleColor);
        this.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
        attributes.recycle();
    }

    @Override
    public void onClick(View view) {
        Context context = getContext();
        if (context == null) {
            return;
        }
        if (context instanceof FragmentActivity) {
            ((FragmentActivity) context).finish();
        }
    }

    public TextView getTvRight() {
        return this.tvRight;
    }

    public TextView getTvLeft() {
        return tvLeft;
    }

    public TextView getTvTitle() {
        return tvTitle;
    }
}
